home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 2000
/
MacHack 2000.toast
/
pc
/
The Hacks
/
Softshoe
/
Lisa's Mac Parts
/
TextEdit
/
TextEditView.cp
< prev
next >
Wrap
Text File
|
2000-06-23
|
6KB
|
289 lines
// TextEditView.cp
#ifndef TextEditView_h
#include "TextEditView.h"
#endif
#ifndef MouseDownEvent_h
#include "MouseDownEvent.h"
#endif
#ifndef CursorObject_h
#include "CursorObject.h"
#endif
#ifndef Canvas_h
#include "Canvas.h"
#endif
#ifndef ValidAndInvalidCanvasLoop_h
#include "ValidAndInvalidCanvasLoop.h"
#endif
#ifndef FirstCanvas_h
#include "FirstCanvas.h"
#endif
TextEditView::TextEditView( GrafPortObject& port,
Focus& focus )
: Activator( focus ),
CommandHandler<Typing>( focus ),
CommandHandler<ReturnKeys>( focus ),
CommandHandler<HorizontalArrows>( focus ),
CommandHandler<VerticalArrows>( focus ),
CommandHandler<DeleteKeys>( focus ),
CommandHandler<Editing>( focus ),
CommandHandler<Selecting>( focus ),
editor( port, Rectangle( 0, 0, 530, 530 ), 530 ),
idle( *this, &TextEditView::Idle, 1000, false )
{
editor.ScrollAutomatically();
}
void TextEditView::AdjustToCanvas( const Canvas& canvas ) const
{
TextEditObject& mutableEditor = const_cast<TextEditObject&>( editor );
mutableEditor.DisplayAt( canvas.Port(),
canvas.OnScreen(),
canvas.OffScreen().TopLeft().AsPointObject() );
}
void TextEditView::Draw( const Canvas& canvas ) const
{
AdjustToCanvas( canvas );
uint32 textHeight = (uint32)editor.LineCount() * editor.LineHeight();
uint32 scroll = editor.Scroll().v;
if ( textHeight <= scroll )
{
EraseRect( &canvas.OnScreen() );
return;
}
textHeight -= scroll;
if ( textHeight < canvas.OnScreen().Height() )
{
Rectangle toErase( canvas.OnScreen() );
toErase.top += textHeight;
EraseRect( &toErase );
}
editor.Draw( canvas.OnScreen() );
}
void TextEditView::Click( const Canvas& clicked,
const MouseDownEvent& event )
{
AdjustToCanvas( clicked );
editor.Click( event.LocalPoint(), event.Shift() );
for ( ValidAndInvalidCanvasLoop canvas( *this );
canvas.Unfinished();
canvas++ )
if ( *canvas != clicked )
canvas->Invalidate();
}
const CursorObject& TextEditView::Cursor( const Canvas&,
const MouseEvent&,
RegionObject& ) const
{
return CursorObject::IBeam();
}
void TextEditView::Activate()
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.Activate();
idle.Enable();
idle.SetPeriod( editor.IdlePeriod() );
}
void TextEditView::Deactivate()
{
idle.Disable();
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.Deactivate();
}
void TextEditView::Idle()
{
if ( !editor.Selection().IsEmpty() )
return;
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.Idle();
}
void TextEditView::Type( ConstData text )
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.ReplaceSelection( text );
editor.RevealSelection();
}
void TextEditView::ReturnKey( const KeystrokeEvent& )
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.TypeReturn();
}
void TextEditView::LeftArrow( const KeystrokeEvent& )
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.LeftArrow();
}
void TextEditView::RightArrow( const KeystrokeEvent& )
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.RightArrow();
}
void TextEditView::UpArrow( const KeystrokeEvent& )
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.UpArrow();
}
void TextEditView::DownArrow( const KeystrokeEvent& )
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.DownArrow();
}
void TextEditView::DeleteBackward( const KeystrokeEvent& )
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.DeleteBackward();
}
void TextEditView::DeleteForward( const KeystrokeEvent& )
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.DeleteForward();
}
bool TextEditView::CanCut()
{
return !editor.Selection().IsEmpty();
}
bool TextEditView::CanCopy()
{
return !editor.Selection().IsEmpty();
}
bool TextEditView::CanPaste()
{
return editor.CanPaste();
}
bool TextEditView::CanClear()
{
return !editor.Selection().IsEmpty();
}
void TextEditView::Cut()
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.Cut();
}
void TextEditView::Copy()
{
editor.Copy();
}
void TextEditView::Paste()
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.Paste();
}
void TextEditView::Clear()
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.DeleteSelection();
}
bool TextEditView::CanSelectAll() const
{
return editor.Selection() != editor.AllSelection();
}
void TextEditView::SelectAll()
{
FirstCanvas canvas( *this );
AdjustToCanvas( *canvas );
editor.SetSelection( editor.AllSelection() );
}
int32 TextEditView::BestWidth( Range32 bounds ) const
{
return bounds.WeaklyRestrict( 530 );
}
int32 TextEditView::BestHeight( Range32 bounds ) const
{
uint16 wrap = editor.WrapWidth();
TextEditObject& mutableEditor = const_cast<TextEditObject&>( editor );
mutableEditor.SetWrapWidth( 530 );
mutableEditor.Recalculate();
int32 lineCount = editor.LineCount();
if ( lineCount == 0 )
lineCount = 1;
int32 best = (lineCount + 2) * editor.LineHeight();
mutableEditor.SetWrapWidth( wrap );
mutableEditor.Recalculate();
if ( best < bounds.Start() )
{
best = bounds.Start() + editor.LineHeight() - 1;
best -= best % editor.LineHeight();
if ( best <= bounds.End() )
return best;
return bounds.Start();
}
if ( best > bounds.End() )
{
best = bounds.End();
best -= best % editor.LineHeight();
if ( best > 0 && best >= bounds.Start() )
return best;
return bounds.End();
}
return best;
}
int32 TextEditView::MinimumHeight() const
{
return editor.LineHeight();
}